package cn.com.bluemoon.daps.model.core.file.entry;

import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.model.core.file.transformer.ImportEntryTableDto;
import cn.com.bluemoon.daps.model.service.DapDataModelSubordinateKeyService;
import cn.com.bluemoon.daps.model.service.DapDataModelTableFieldService;
import cn.com.bluemoon.daps.model.service.DapDataModelTablePartitionService;
import cn.com.bluemoon.daps.model.service.DapDataModelTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Jarod.Kong
 * @date 2021/3/11 15:23
 */
@Service
@Slf4j
public class ResultEntryImpl implements ResultEntry<ImportEntryTableDto> {

    @Autowired
    private DapDataModelTableService tableService;

    @Autowired
    private DapDataModelTableFieldService fieldService;

    @Autowired
    private DapDataModelSubordinateKeyService indexService;

    @Autowired
    private DapDataModelTablePartitionService partitionService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultBean<Boolean> entry(ImportEntryTableDto entryModel) {
        tableService.save(entryModel.getTable());
        fieldService.bmSaveBatch(entryModel.getFields());
        indexService.bmSaveBatch(entryModel.getIndexes());
        partitionService.bmSaveBatch(entryModel.getPartitions());
        return ResultBean.ok(true);
    }
}
